package com.edu.koudingmusic.activities;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.edu.koudingmusic.BaseActivity;
import com.edu.koudingmusic.BaseApplication;
import com.edu.koudingmusic.R;
import com.edu.koudingmusic.adapters.MyMusicListAdapter;
import com.edu.koudingmusic.entities.Mp3Info;
import com.edu.koudingmusic.services.PlayService;
import com.edu.koudingmusic.tools.Constant;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class PlayRecordListActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private static final String TAG = "PlayRecordListActivity";
    private ListView mListView;
    private TextView mTextView;
    private BaseApplication mApp;
    private ArrayList<Mp3Info> mMp3Infos;
    private MyMusicListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_record_list);

        mApp = ((BaseApplication) getApplication());
        mListView = (ListView) findViewById(R.id.listView_play_record);
        mTextView = (TextView) findViewById(R.id.play_no_record);
        mListView.setOnItemClickListener(this);
        initData();
    }


    @Override
    protected void onResume() {
        super.onResume();
        bindPlayService();
    }

    @Override
    protected void onPause() {
        super.onPause();
        unBindPlayService();
    }

    // 初始化数据
    private void initData() {

        try {
            // 查询最近播放的记录
            List<Mp3Info> mp3InfoList = mApp.sDbUtils.findAll(Selector.from(Mp3Info.class).where("playTime", "!=", 0).orderBy("playTime", true).limit(Constant.PLAY_RECORD_NUM));
            Log.e(TAG, "initData: " + mp3InfoList);
            if (mp3InfoList == null || mp3InfoList.size() == 0) {
                mTextView.setVisibility(View.VISIBLE);
                mListView.setVisibility(View.GONE);
            } else {
                mTextView.setVisibility(View.GONE);
                mListView.setVisibility(View.VISIBLE);

                mMp3Infos = ((ArrayList<Mp3Info>) mp3InfoList);
                mAdapter = new MyMusicListAdapter(this, mMp3Infos);
                mListView.setAdapter(mAdapter);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void publish(int progress) {

    }

    @Override
    public void change(int position) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mPlayService.getChangeList()!= PlayService.MY_PLAY_RECORD_MUSIC_LIST){
            mPlayService.setMp3Infos(mMp3Infos);
            mPlayService.setChangeList(PlayService.MY_PLAY_RECORD_MUSIC_LIST);
        }
        mPlayService.play(position);

    }
}
